package com.tcps.wechat_push.job;//package com.tcps.pushwechat.job;

import com.tcps.wechat_push.entity.Jobbean;
import com.tcps.wechat_push.service.impl.WeChatService;
import com.tcps.wechat_push.util.SpringContextUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2018-07-17 17:10
 * Copyright 2018  by 三水林科技有限公司
 **/
//@Component
public class PushJob implements Job,Serializable {
    /**
     * Logger
     */
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    private WeChatService weChatService = (WeChatService) SpringContextUtils.getBean("weChatService");


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Jobbean job = (Jobbean) context.getMergedJobDataMap().get("scheduleJob");
        LOGGER.info("执行任务={}","执行任务中。。。。。"+job.getId());
        List list = weChatService.queryPlan();
        RestTemplate template = new RestTemplate();
        String url = "http://192.168.2.40:8081/channel/channelHourData/getHourNewUserData";
        // 封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
        paramMap.add("dt", "20180416");

        // 1、使用postForObject请求接口
        String result = template.postForObject(url, paramMap, String.class);
        System.out.println("result1==================" + result);

        // 2、使用postForEntity请求接口
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap,headers);
        ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class);
        System.out.println("result2====================" + response2.getBody());

        // 3、使用exchange请求接口
        ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
        System.out.println("result3====================" + response3.getBody());

    }
}
